﻿using Kingdee.BOS.Core.CommonFilter.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.PreInsertData.DataType;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Model.Base;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.ServiceHelper.ManagementCenter;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.FilterPlugin.BPW
{
    [Description("简单账表过滤插件，过滤基础资料"), HotUpdate]
    public class AgsReportFilter : AbstractCommonFilterPlugIn
    {
        public override void BeforeF7Select(BeforeF7SelectEventArgs e)
        {
            base.BeforeF7Select(e);
            // 给成本中心查询附加过滤条件
            if (e.FieldKey.EqualsIgnoreCase("F_POSW_BaseDep") && e.FormId.EqualsIgnoreCase("BD_Department"))
            {
                e.ListFilterParameter.Filter = e.ListFilterParameter.Filter.JoinFilterString(" FDEPTPROPERTY = '4866f13a3a3940b9b2fe47895a6e7cbe' ");
                return;
            }
            //给物料查询附加过滤条件
            if (e.FieldKey.EqualsIgnoreCase("F_POSW_BaseProduct") && e.FormId.EqualsIgnoreCase("BD_MATERIAL"))
            {
                e.ListFilterParameter.Filter = e.ListFilterParameter.Filter.JoinFilterString(" FISPRODUCE = 1 ");
                return;
            }
            if (e.FieldKey.EqualsIgnoreCase("F_POSW_BaseProduct2") && e.FormId.EqualsIgnoreCase("BD_MATERIAL"))
            {
                e.ListFilterParameter.Filter = e.ListFilterParameter.Filter.JoinFilterString(" FISPRODUCE = 1 ");
                return;
            }
        }
        public override void AfterBindData(EventArgs e)
        {
            base.AfterBindData(e);
            List<EnumItem> enumList = GetEnumLists();
            var comboList = this.View.GetFieldEditor<ComboFieldEditor>("F_POSW_ComboPeriod", 0);
            if (comboList != null)
            {
                comboList.SetComboItems(enumList);
            }
        }

        private List<EnumItem> GetEnumLists()
        {
            List<EnumItem> enumItems = new List<EnumItem>();
            string sql = string.Format(@"/*dialect*/ 
            SELECT DISTINCT 
                   CAST(T1.FYEAR AS CHAR(4)) + CASE
                                                   WHEN LEN(CAST(T1.FPERIOD AS CHAR(2))) = 1
                                                   THEN '0' + CAST(T1.FPERIOD AS CHAR(2))
                                                   ELSE CAST(T1.FPERIOD AS CHAR(2))
                                               END FPERIOD
            FROM T_BD_ACCOUNTPERIOD T1
            WHERE 1 = 1
                  AND YEAR(T1.FPERIODSTARTDATE) >= YEAR(GETDATE()) - 1
                  AND YEAR(T1.FPERIODSTARTDATE) < YEAR(GETDATE()) + 2 ");
            var ds = DBServiceHelper.ExecuteDataSet(this.Context, sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    string item = ds.Tables[0].Rows[i][0].ToString();
                    var enumItem = new EnumItem();
                    enumItem.Caption = new Kingdee.BOS.LocaleValue(item);
                    enumItem.Value = item;
                    enumItems.Add(enumItem);
                }
            }
            return enumItems;
        }
    }
}
